# - Coding UTF8 -
#
# Networked Decision Making
# Site: http://code.google.com/p/global-decision-making-system/
#
# License Code: GPL, General Public License v. 2.0
# License Content: Creative Commons Attribution 3.0
#
# Also visit: www.web2py.com
#             or Groups: http://groups.google.com/group/web2py 
# 	For details on the web framework used for this development
#
# Developed by Russ King (newglobalstrategy@gmail.com
# Russ also blogs occasionally to pass the time at: 
# http://proudofyourplanent.blogspot.com
# His general thinking on why this project is very important is available at
# http://www.scribd.com/doc/98216626/New-Global-Strategy
# With thanks to Guido, Massimo and many other that make this sort of thing
# much easier than it used to be
# This controller has 3 functions:
# my_questions for reviewing progress on questions you have asked
# my_answers for reviewing your answers
# resovled for reviewing resolved questio

'''This controller has 3 functiosns:

my_questions - for reviewing questions you have submitted
my_answers - for questions you have answered
resolved - for reviewing resolved questions
'''



@auth.requires_login()    
def my_questions():
    # moved to resolved approach but different query
    if len(request.args): 
        page=int(request.args[0])
    else: page=0
    items_per_page=20
    limitby=(page*items_per_page,(page+1)*items_per_page+1)

    quests=db((db.question.auth_userid == auth.user.id) & (db.question.qtype == 
               'quest')).select(db.question.id,
              db.question.questiontext,db.question.status,db.question.level,
              db.question.urgency, db.question.importance,  
              db.question.correctanstext, orderby=[~db.question.createdate],limitby=limitby)

    return dict(quests=quests,page=page,items_per_page=items_per_page)

@auth.requires_login() 
def my_answers():

    fields= ['asortorder','showscope','scope', 'continent','country','subdivision',
             'showcat','category']
    form = SQLFORM(db.viewscope, fields=fields)

    page = 0
    q = 'Que'
    s = 'Resolved'

    if session.showscope is None:
        form.vars.showscope = False
        form.vars.showcat = False
    else:
        form.vars.showscope = session.showscope
        form.vars.showcat = session.showcat
        form.vars.category = session.category
        form.vars.scope = session.scope
        form.vars.continent = session.vwcontinent
        form.vars.country = session.vwcountry
        form.vars.subdivision = session.vwsubdivision
    
    if session.sortorder is not None:
        form.vars.asortorder = session.sortorder

    if len(request.args): 
        page=int(request.args[0])
        if len(request.args)>1:
            q = request.args[1]
            if len(request.args)>2:
                s = request.args[2]
    else: page=0

    items_per_page=10
    limitby=(page*items_per_page,(page+1)*items_per_page+1)


    if session.sortorder is not None:   
        if session.sortorder == '1 Answer Date':
            s = 'Answer'
        elif session.sortorder == '2 Resolved Date':
            s = 'Resolved'
        elif session.sortorder == '3 Category':
            s = 'Category'

    if form.validate():
        session.showcat = form.vars.showcat
        session.showscope = form.vars.showscope
        session.scope = form.vars.scope
        session.category = form.vars.category

        session.vwcontinent = form.vars.continent
        session.vwcountry = form.vars.country
        session.vwsubdivision = form.vars.subdivision
        session.sortorder = form.vars.asortorder
 
        if session.sortorder == '1 Answer Date':
            s = 'Answer'
        elif session.sortorder == '2 Resolved Date':
            s = 'Resolved'
        elif session.sortorder == '3 Category':
            s = 'Category'


        page = 0
        redirect(URL('my_answers',args=[page,q,s]))

    # Actions can be selected for all or status of Agreed, In Progress or Disagreed
    # Rejected actions cannot be reviewed

    query = (db.userquestion.auth_userid == auth.user.id)
    if q == 'Resolved':
        query = query & (db.userquestion.status == 'Resolved')
    elif q == 'InProg':  # we are not showing this for philosophical reasons at the moment
        query = query & (db.userquestion.status == 'In Progress')

    if session.showcat == True:
        query = query & (db.userquestion.category == session.category)
    if session.showscope == True:
        query = query & (db.userquestion.activescope == session.scope)  
        if session.scope == '1 Global':
            query = query & (db.userquestion.activescope == session.scope)   
        elif session.scope == '2 Continental':
            query = query & (db.userquestion.activescope == session.scope) & (
            db.userquestion.continent == session.vwcontinent)
        elif session.scope == '3 National':
            query = query & (db.userquestion.activescope == session.scope) & (
            db.userquestion.country == session.vwcountry)
        elif session.scope == '4 Local':
            query = query & (db.userquestion.activescope == session.scope) & (
            db.userquestion.subdivision == session.vwsubdivision)

    # And they can be sorted by create date, priority and due date    
    sortby = ~db.userquestion.ansdate

    if s == 'Resolved':
        sortby = ~db.userquestion.resolvedate
    elif s == 'Category':
        sortby = db.userquestion.category


    quests=db(query).select(db.userquestion.id,db.userquestion.status,
              db.userquestion.questionid, db.userquestion.answer, db.userquestion.score,
              orderby=[sortby],limitby=limitby)

    return dict(form=form,quests=quests,page=page,items_per_page=items_per_page,q=q,s=s,query=query)


def resolved():

    fields= ['qsortorder','showscope','scope', 'continent','country','subdivision',
             'showcat','category']
    form = SQLFORM(db.viewscope, fields=fields)

    page = 0
    q = 'Que'
    s = 'Resolved'
    #form.vars.showscope = False
    #form.vars.showcat = False
    if session.showscope is None:
        form.vars.showscope = False
        form.vars.showcat = False
    else:
        form.vars.showscope = session.showscope
        form.vars.showcat = session.showcat
        form.vars.category = session.category
        form.vars.scope = session.scope
        form.vars.continent = session.vwcontinent
        form.vars.country = session.vwcountry
        form.vars.subdivision = session.vwsubdivision
    
    if session.sortorder is not None:
        form.vars.qsortorder = session.sortorder

    if len(request.args): 
        page=int(request.args[0])
        if len(request.args)>1:
            q = request.args[1]
            if len(request.args)>2:
                s = request.args[2]
    else: page=0

    items_per_page=20
    limitby=(page*items_per_page,(page+1)*items_per_page+1)


    if session.sortorder is not None:   
        if session.sortorder == '1 Priority':
            s = 'Priority'
        elif session.sortorder == '2 Resolved Date':
            s = 'Resolved'
        elif session.sortorder == '3 Submit Date':
            s = 'Submit'


    if form.validate():
        session.showcat = form.vars.showcat
        session.showscope = form.vars.showscope
        session.scope = form.vars.scope
        session.category = form.vars.category
        session.vwcontinent = form.vars.continent
        session.vwcountry = form.vars.country
        session.vwsubdivision = form.vars.subdivision
        session.sortorder = form.vars.qsortorder
 
        if session.sortorder == '1 Priority':
            s = 'Priority'
        elif session.sortorder == '2 Resolved Date':
            s = 'Resolved'
        elif session.sortorder == '3 Submit Date':
            s = 'Submit'


        page = 0
        redirect(URL('resolved',args=[page,q,s]))

    # Actions can be selected for all or status of Agreed, In Progress or Disagreed
    # Rejected actions cannot be reviewed

    query = (db.question.qtype == 'quest') & (db.question.status == 'Resolved')
    if q == 'Que':
        query = (db.question.qtype == 'quest') & (db.question.status == 'Resolved')

    if q == 'Reject':  #we might show this and perhaps even allow challenges
        query = (db.question.qtype == 'quest') & (db.question.status == 'Rejected')
    elif q == 'InProg':  # we are not showing this for philosophical reasons at the moment
        query = (db.question.qtype == 'quest') & (db.question.status == 'Resolved')
    elif q == 'My' and auth.user is not None:
        query = (db.question.auth_userid == auth.user.id) & (db.question.qtype == 'quest')

    if session.showcat == True:
        query = query & (db.question.category == session.category)
    if session.showscope == True:
        query = query & (db.question.activescope == session.scope)  
        if session.scope == '1 Global':
            query = query & (db.question.activescope == session.scope)   
        elif session.scope == '2 Continental':
            query = query & (db.question.activescope == session.scope) & (
            db.question.continent == session.vwcontinent)
        elif session.scope == '3 National':
            query = query & (db.question.activescope == session.scope) & (
            db.question.country == session.vwcountry)
        elif session.scope == '4 Local':
            query = query & (db.question.activescope == session.scope) & (
            db.question.subdivision == session.vwsubdivision)

    # And they can be sorted by create date, priority and due date    
    sortby = ~db.question.priority
    if s == 'Resolved':
        sortby = ~db.question.resolvedate
    elif s == 'Submit':
        sortby = ~db.question.createdate


    quests=db(query).select(db.question.id,
              db.question.questiontext, db.question.status,db.question.level, 
              db.question.priority, db.question.correctanstext, 
              db.question.numagree, db.question.numdisagree,
              orderby=[sortby],limitby=limitby)

    return dict(form=form,quests=quests,page=page,items_per_page=items_per_page,q=q,s=s,query=query)




